package com.qf.openapi.web.master.controller;

import com.spy.openapi.web.master.utils.Constants;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Controller
public class DownloadConterllor {
    @GetMapping("/download")
    public void download(String filename, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.
        String path = request.getServletContext().getRealPath(Constants.UPLOAD_PATH);
        String string  = path + File.separator + filename;
        File file = new File(string);
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+filename);
        IOUtils.copy(new FileInputStream(file),response.getOutputStream());
    }
}
